import Vue from 'vue'
import Vuex from 'vuex'
import { setToken,getToken } from '@/utils/token'
import { login } from '@/api/api'
Vue.use(Vuex)

export default new Vuex.Store({
  // state: 存放变量
  state: {
     //取token
     token: getToken() || ''
  },
  // mutation: 存放方法
  mutations: {
    doLogin(state, newToken) {
      //更新token
      state.token = newToken
      //存token
      setToken(newToken)
    }
  },
  actions: {
    async login(context, payload) {
      //发送请求获取token
      const { data } = await login(payload)
      context.commit('doLogin', data.data.token)
    }
  }
})